<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/pages.php
#	FUNCTION:		Handling the Pages and Page classes, as well as page-related database functions
#	AUTHOR:			Adrian Liu
#	Modified:		25/07/2011
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

include "page.php";
include "pagedata.php";
include "pageview.php";

/**
 * Pages() contains Page objects for the pages in the site
*/
class Pages {

	# Array of pages
	private $page_array;
	
	/**
	*constructor of Pages
	*
	*/
	function __construct() {
		
		$this -> page_array = array();
	}
	
	/**
	*destructor of Pages
	*/
	function __destruct(){
		
		unset($this -> page_array);
	}
	
	/**
	*
	*
	*/
	public function isPageExist($pageID){
		
		if(array_key_exists($pageID,$this -> page_array)){
			
			return true;
		}
		
		return false;
	}
	
	public function loadNavigationPages($section = ""){
		
		//header('Content-type: application/json');
		
		$objPageData = new PageData();
		
		return $objPageData -> getNavigationPages($section);

	}
	
	/**
	 * getPageAtDepth() gets the page in a passed page ID at a passed depth
	 *
	 * @param page_id is the ID of the page to use as a path to
	 * @param depth is the depth to get the page at
	 *
	 * @return a page ID if found, or false if not
	*/
	function getPageAtDepth($page_id = "", $depth = 0) {

		//# If ID exists
//		if (array_key_exists($page_id, $this -> page_array)) {
//
//			# Get path
//			$page_path = $this -> getPathToPage($page_id);
//
//			# If depth exists, return it
//			if (array_key_exists($depth, $page_path)) {
//
//				return $page_path[$depth];
//
//			} else {
//
//				return false;
//			}
//
//		# ID doesn't exist
//		} else {
//
//			return false;
//		}

		$objPageData = new PageData();
		
		return $objPageData -> getPageAtDepth($page_id,$depth);
	}

	/**
	 * Pages() is the constructor.
	 *
	 * @param base_page_array is the array of pages to add on instantiation
	*/
	//function Pages($base_page_array = array()) {
//
//		# For each page
//		foreach ($base_page_array as $key => $page) {
//
//			# Add the page
//			$this -> addPage($page);
//		}
//	}
	
	/**
	 * addPage() adds a page
	 *
	 * @param page is the page object to add
	*/
	function addPage($page) {

		# If page passed
		if ($page) {
		
			# If doesn't already exist
			if ($this -> getPage($page -> getID()) === false) {

				# Add
				$this -> page_array[$page -> getID()] = $page;

			# Add error
			} else {
								
				$this -> page_array[$page -> getID()] -> loadPageData();
				//addError("Pages -> addPage(): Page '" . $page -> getID() . "' already exists, can't be added again.");
			}
		}
	}

	/**
	 * removePage() removes a page by ID
	 *
	 * @param page_id is the ID of the page
	*/
	function removePage($page_id = "") {


	}

	/**
	 * getPage() return a page object
	*/
	function getPage($page_id = "") {

		if (is_string($page_id) || is_integer($page_id)) {			

			# If set
			if (array_key_exists($page_id, $this -> page_array)) {

				# Return page object
				return $this -> page_array[$page_id];

			} else {

				# Return failure
				return false;
			}
		} else {

			return false;
		}
	}

	/**
	 * getPagesWithValues() gets an array of pages that have the passed values for the passed keys
	 *
	 * @param key_array is an array of keys
	 * @param value_array is an array of values
	 *
	 * @return an array of Pages
	*/
	function getPagesWithValues($key_array = array(), $value_array = array()) {

		# Array to return
		$page_array = array();

		# If any pages found...
		if (count($this -> page_array) > 0) {

			# For each page
			foreach ($this -> page_array as $page_id => $page) {

				# All values matching
				$values_matching = true;

				# For each key
				foreach ($key_array as $index => $key) {

					# If not matching...
					if ($page -> getData($key) != $value_array[$index]) {

						$values_matching = false;
					}
				}

				# If all values matching
				if ($values_matching) {

					# Add page
					$page_array[$page_id] = $page;
				}
			}
		}

		# Return
		return $page_array;
	}

	/**
	 * printPages() returns a print out of all the current pages
	 *
	 * @return a print out of the pages
	*/
	function printPages() {

		# Output
		$output = "<h2>printPages (" . count($this -> page_array) . " pages)</h2>";

		# If any pages found...
		if (count($this -> page_array) > 0) {

			# For each page
			foreach ($this -> page_array as $page_id => $page) {

				$output .= $this -> printPage($page_id);
			}
		}

		# Return
		return $output;
	}

	/**
	 * printPage() prints a particular page's details
	 *
	 * @param page_id is the ID of the page to print
	 *
	 * @return a print out of the page
	*/
	function printPage($page_id = "") {

		# Output
		$output = "";

		if (array_key_exists($page_id, $this -> page_array)) {

			$page = $this -> page_array[$page_id];

			$output = $page -> printPage();
		}

		# Return
		return $output;
	}

	/**
	 * getPageIDs() gets an array of page IDs in the Pages object
	 *
	 * @return an array of IDs
	*/
	function getPageIDs() {

		# For each page
		return array_keys($this -> page_array);
	}


	/**
	 * getPathToPage() gets an array of top level to the passed page ID
	 *
	 * @param page_id is the ID of the page to get the path to
	 *
	 * @return an array of page IDs from top level to passed page ID, or false if ID not found
	*/
	function getPathToPage($page_id = "") {

		# Array
		$page_array = array();

		# If ID exists
		if (array_key_exists($page_id, $this -> page_array)) {

			# Add passed page as first item
			$page_array[] = $page_id;

			# Get reference to page
			$page = $this -> page_array[$page_id];

			# Cycle through while parent is not nothing
			while ($page -> getData("p_parent") != "") {

				# Set parent
				$page_id = $page -> getData("p_parent");

				# Add
				$page_array[] = $page_id;

				# Get reference to new page
				$page = $this -> page_array[$page_id];

			}

			# Reverse array so top level is first
			$page_array = array_reverse($page_array);

			# Return
			return $page_array;

		# If doesn't exist
		} else {

			return false;
		}
	}

	


	/**
	 * getChildrenOfPage() gets the children of a passed page
	 *
	 * @param page_id is the page to get the children of
	 *
	 * @return a pages object containing the children page
	*/
	function getChildrenOfPage($page_id = "") {

		# Pages array to return
		$pages = new Pages();

		# Get pages without this parent
		$pages_array = $this -> getPagesWithValues(array("p_parent"), array($page_id));

		# Add to the Pages object
		foreach ($pages_array as $key => $page) {

			# Add
			$pages -> addPage($page);
		}

		# Return
		return $pages;
	}


	/**
	 * getAuthorizedGroups() gets an array of groups authorized to see the passed page
	 *
	 * @param page_id is the ID of the page to check
	 *
	 * @return an array of group IDs
	*/
	function getAuthorizedGroups($page_id = "") {

		# List of group IDs (comes in comma-delimited string)
		$group_id_array = "";
		# If ID passed
		if ($page_id != "" && isset($this -> page_array[$page_id])) {

			$page = new Page();
			
			# Get reference to page
			$page = $this -> page_array[$page_id];

			# Cycle through while parent is not nothing
			while ($page -> getData("p_parent") != "" && $page -> getData("p_accesstype") == "0") {

				# Set parent
				$page_id = $page -> getData("p_parent");

				# Get reference to new page
				$page = $this -> page_array[$page_id];
			}

			# At level where groups are defined...
			$group_id_array = array_keys($page -> getData("p_access"));
		}

		# Return exploded string
		if ($group_id_array != "") {
			return $group_id_array;
		} else {
			return array();
		}
	}

//	/**
//	 * getAuthorizedGroups() gets an array of groups authorized to see the passed page
//	 *
//	 * @param page_id is the ID of the page to check
//	 *
//	 * @return an array of group IDs
//	*/
//	function getAuthorizedGroupsForFront($page_id = "") {
//
//		# List of group IDs (comes in comma-delimited string)
//		$group_id_array = "";
//		# If ID passed
//		if ($page_id != "" && isset($this -> page_array[$page_id])) {
//
//			# Get reference to page
//			$page = $this -> page_array[$page_id];
//
//			# Cycle through while parent is not nothing
//			while ($page["p_parent"] != "" && $page["p_accesstype"] == "0") {
//
//				# Set parent
//				$page_id = $page["p_parent"];
//
//				# Get reference to new page
//				$page = $this -> page_array[$page_id];
//			}
//
//			# At level where groups are defined...
//			$group_id_array = $page -> getData("p_access");
//		}
//
//		# Return exploded string
//		if ($group_id_array != "") {
//			return explode(",", $group_id_array);
//		} else {
//			return array();
//		}
//	}
	
	/**
	 * getAuthorizedGroupNames() gets an array of group names and IDs allowed to see the passed page
	 *
	 * @param page_id is the ID of the page to check
	 *
	 * @return an array of group names and IDs
	*/
	function getAuthorizedGroupNames($page_id = "") {

		# Globals
		global $PROJECT_DB_TABLES;

		# Get IDs of groups for this page
		$group_id_array = $this -> getAuthorizedGroups($page_id);

		# Create SQL
		if (count($group_id_array) > 0) {
			$sql = "SELECT g_id, g_name FROM " . $PROJECT_DB_TABLES["groups"] . " WHERE ";
			foreach ($group_id_array as $group_counter => $group_id) {
				$sql .= "g_id = " . $group_id;
				if ($group_counter < count($group_id_array) - 1) {
					$sql .= " OR ";
				}
			}

			# Get results
			$group_name_array = getRecord($sql);

			# Merge
			$group_array = array();
			foreach ($group_name_array as $group) {
				$group_array[] = array(
					"id" => $group["g_id"],
					"name" => $group["g_name"]
				);
			}

			# Return
			return $group_array;

		} else {
			return array();
		}
	}


	/**
	 * getPagesArray() returns the pages array
	 *
	 * @return the pages array
	*/
	function getPagesArray() {
		return $this -> page_array;
	}
}

?>